﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Table.DataModel;
using Table;
using OneP.InfinityScrollView;

namespace UI
{
    public class AutoChessExplainPageHeroCell : InfinityBaseItem
    {

        public TextureKit heroIcon;
        public GroupSpriteKit heroIconFrame;
        public Text heroName;
        public Text cost;
        public GameObject buffIcon;
        public Transform buffIconParent;
        public GameObject skillIcon;
        public Transform skillIconParent;

        /// <summary>
        /// 英雄最高的星级
        /// </summary>
        public int heroMaxStarLevel = 3;

        public override void Reload(InfinityScrollView infinity, int _index)
        {
            base.Reload(infinity, _index);
            SetContent(AutoChessHerosSubPage.HerosNeedToShow[_index]);
        }

        /// <summary>
        /// 根据英雄 id, 设置每一个英雄项的内容
        /// </summary>
        /// <param name="autoChessHeroId"></param>
        public void SetContent(AutoChessHeroQueryData heroData)
        {

            // ----------------------------------UI---------------------------------------------//
            // 英雄头像、英雄名、头像框
            TableHero tableHeroData = TableDataManager.Instance.tableHeroDao.GetById(heroData.heroId);
            heroIcon.TextureName = tableHeroData.heroPaint;
            heroName.text = tableHeroData.heroName;
            switch (heroData.rare)
            {
                default:
                case 1:
                    heroIconFrame.SetSpriteName("yunying_di_hui");
                    break;
                case 2:
                    heroIconFrame.SetSpriteName("yunying_di_lv");
                    break;
                case 3:
                    heroIconFrame.SetSpriteName("yunying_di_lan");
                    break;
                case 4:
                    heroIconFrame.SetSpriteName("yunying_di_zi");
                    break;
                case 5:
                    heroIconFrame.SetSpriteName("yunying_di_cheng");
                    break;
            }

            // 阵营和职业的 buff
            buffIconParent.DestroyChildren();
            foreach (var group in heroData.groupId)
            {
                TableAutoChessGroup groupData = TableDataManager.Instance.tableAutoChessGroupDao.GetById(group);
                InstantiateBuffIcon(groupData);
            }

            //foreach (var job in heroData.jobs)
            //{
            //    TableAutoChessGroup groupData = TableDataManager.Instance.tableAutoChessGroupDao.GetById(job);
            //    InstantiateBuffIcon(groupData);
            //}

            // 英雄价格（使用一星英雄的值）、技能
            // 需要遍历所有星级的英雄棋数据
            bool skill1Unlocked = false;
            bool skill2Unlocked = false;
            bool skill3Unlocked = false;
            //bool skill4Unlocked = false;
            bool ultimateUnlocked = false;
            skillIconParent.DestroyChildren();
            // 现在技能图标的顺序有可能乱序（第一个图标不是一技能、第二个图标不是二技能...）
            for (int i = 0; i < heroMaxStarLevel - 1; i++)
            {
                int curChessId = heroData.chessId + i;
                TableAutoChessHeroChess heroChessData = TableDataManager.Instance.tableAutoChessHeroChessDao.GetById(curChessId);

                // 价格，使用一星英雄的
                if (i == 0)
                {
                    cost.text = heroChessData.buyPrice.ToString();
                }

                // 一技能是否 i 星解锁？
                if (heroChessData.skill1 != 0 && !skill1Unlocked)
                {
                    skill1Unlocked = true;
                    InstantiateSkillIcon(heroChessData.skill1, i + 1);
                }
                // 二技能是否 i 星解锁？
                if (heroChessData.skill2 != 0 && !skill2Unlocked)
                {
                    skill2Unlocked = true;
                    InstantiateSkillIcon(heroChessData.skill2, i + 1);
                }
                // 三技能是否 i 星解锁？
                if (heroChessData.skill3 != 0 && !skill3Unlocked)
                {
                    skill3Unlocked = true;
                    InstantiateSkillIcon(heroChessData.skill3, i + 1);
                }
                // 四技能，暂时没有
                //if (heroChessData.skill4 != 0)
                //{

                //}
                // 奥义是否 i 星解锁？
                if (heroChessData.ultimateSkill != 0 && !ultimateUnlocked)
                {
                    ultimateUnlocked = true;
                    InstantiateSkillIcon(heroChessData.ultimateSkill, i + 1);
                }

            }
        }

        /// <summary>
        /// 实例化 buff icon
        /// </summary>
        private void InstantiateBuffIcon(TableAutoChessGroup group)
        {
            GameObject buffIconObj = Instantiate(buffIcon, buffIconParent);
            buffIconObj.SetActive(true);
            buffIconObj.transform.localRotation = Quaternion.identity;
            buffIconObj.transform.localScale = Vector3.one;
            buffIconObj.GetComponent<AutoChessExplainPageHeroBuff>().SetContent(group);
        }

        /// <summary>
        /// 实例化技能 icon
        /// </summary>
        /// <param name="skillId">技能id</param>
        /// <param name="starLevel">节锁星级</param>
        private void InstantiateSkillIcon(int skillId, int starLevel)
        {
            TableSkill skillData = TableDataManager.Instance.tableSkillDao.GetById(skillId);
            GameObject skillIconObj = Instantiate(skillIcon, skillIconParent);
            skillIconObj.SetActive(true);
            skillIconObj.transform.localRotation = Quaternion.identity;
            skillIconObj.transform.localScale = Vector3.one;
            skillIconObj.GetComponent<AutoChessExplainPageHeroSkill>().SetContent(skillData, starLevel);
        }
    }
}
